home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5309 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: chip.bgsu.edu!turner
  2. From: Andrew Turner <turner@chip.bgsu.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: error checking for int
  5. Date: Sat, 3 Feb 1996 12:14:51 -0500
  6. Organization: Bowling Green State University
  7. Message-ID: <Pine.SUN.3.91.960203120230.12797A-100000@chip.bgsu.edu>
  8. NNTP-Posting-Host: chip.bgsu.edu
  9. Mime-Version: 1.0
  10. Content-Type: TEXT/PLAIN; charset=US-ASCII
  11.  
  12.  
  13.   I have an array of ints declared
  14.     
  15.     int something[20];
  16.  
  17. and when I pass it a char it goes into an endless loop, ex.
  18.  
  19.     cout>>"\nEnter a number from 1-20: ";
  20.     cin<<something[0];
  21.  
  22. if the person enters a char then the program goes bonkers.  One 
  23. modification that I have tried is
  24.  
  25.     cout>>"\nEnter a number from 1-20: ";
  26.     cin<<temp;    //where temp is declared as an int.
  27.     while ((temp<1) || (temp >20))
  28.     {
  29.        temp=0;
  30.        cout>>"\nEnter a valid number from 1-20: ";
  31.        cin>>temp;
  32.     }
  33.     something[0]=temp;
  34.  
  35. but all this seems to do is go into a loop that prints 
  36.  
  37.     Enter a valid number from 1-20:
  38.  
  39. to the screen a million times or until I break out of it.
  40.  
  41. The platform that I am having a problem doing this on is CodeWarrior 7 for 
  42. Mac and Borland C++ (not sure what version).
  43.  
  44. Thanks in advance,
  45. andy
  46. ---------------------------------------------------------------------------  
  47.   Andrew Turner  < turner@dad.bgsu.edu > 
  48.   Member: WCNet, BGCC
  49.   Places of Employment:  FSG workroom,  Green Manufacturing, Science Library
  50.   Major:  Mathematics, and Computer Science    Year:  Senior
  51. ----------------------------------------------------------------------------
  52.  
  53.